package com.yy.personalcv.controller;


import com.yy.personalcv.pojo.Education;
import com.yy.personalcv.pojo.Internship;
import com.yy.personalcv.pojo.UserInformation;
import com.yy.personalcv.responce.EducationResponce;
import com.yy.personalcv.responce.InternshipResponce;
import com.yy.personalcv.service.EducationService;
import com.yy.personalcv.service.InternshipService;
import com.yy.personalcv.service.ProjectService;
import com.yy.personalcv.service.SkillService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

@Controller
public class HomeController {

    @Resource
    private SkillService skillService;

    @Resource
    private ProjectService projectService;

    @Resource
    private InternshipService internshipService;

    @Resource
    private EducationService  educationService;

    @RequestMapping(value = {"/", "/home.do"})
    public String home(HttpServletRequest request, Model model) {

        UserInformation userInformation = (UserInformation) request.getSession().getAttribute("userInformation");
        if(userInformation == null){
            return "redirect:/login.do";
        }
        // 查询skill列表
        int uid = userInformation.getId();
        model.addAttribute("skills", skillService.selectAll(uid));
        model.addAttribute("projects", projectService.selectAll(uid));
        List<Internship> internships = internshipService.selectAll(uid);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        List<InternshipResponce> internshipResponses = new ArrayList<> ();
        for(Internship internship : internships){
            InternshipResponce internshipResponce = new InternshipResponce();
            internshipResponce.setId(internship.getId());
            internshipResponce.setCompany(internship.getCompany());
            internshipResponce.setStartTime(sdf.format(internship.getStartTime()));
            internshipResponce.setEndTime(sdf.format(internship.getEndTime()));
            internshipResponce.setDescription(internship.getDescription());
            internshipResponce.setModified(sdf.format(internship.getModified()));
            internshipResponce.setDisplay(internship.getDisplay());
            internshipResponce.setUid(internship.getUid());
            internshipResponses.add(internshipResponce);
        }
        model.addAttribute("internships", internshipResponses);
        List<Education> educations = educationService.selectAll(uid);
        List<EducationResponce> educationResponses = new ArrayList<>();
        for(Education education : educations){
            EducationResponce educationResponce = new EducationResponce();
            educationResponce.setId(education.getId());
            educationResponce.setSchool(education.getSchool());
            educationResponce.setMajor(education.getMajor());
            educationResponce.setDegree(education.getDegree());
            educationResponce.setStartTime(sdf.format(education.getStart()));
            educationResponce.setEndTime(sdf.format(education.getEnd()));
            educationResponce.setModified(sdf.format(education.getModified()));
            educationResponce.setDisplay(education.getDisplay());
            educationResponce.setUid(education.getUid());
            educationResponses.add(educationResponce);
        }
        model.addAttribute("educations", educationResponses);
        model.addAttribute("userInformation",userInformation);
        return "home_page";
    }
}
